﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.DAL.Report;

namespace HRM.UI.HRManagerUI.EvaluationUI
{
    public partial class PromotionEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDesignation();
            }
        }
        private void LoadDesignation()
        {
            List<Designation> designations = new List<Designation>();
            DesignationManager manager = new DesignationManager();
            designations = manager.GetAllDesignations();
            selectDesig.DataTextField = "Title";
            selectDesig.DataValueField = "Title";
            selectDesig.DataSource = designations;
            selectDesig.DataBind();
            selectDesig.Items.Insert(0, "---Choose Any---");
        }
        protected void SearchEmployee(object o, EventArgs e)
        {
            PopulateEmployeeInfo();
        }
        private void PopulateEmployeeInfo()
        {
            try
            {
                firstinfoTb.Visible = true;
                punishment.Visible = true;
                promotedTb.Visible = true;
                promotionHistoryTb.Visible = true;
                imageDiv.Visible = true;
                //Employee General Info
                lblSearch.Text = "";
                Code code = new Code();
                code.CodeName = txtEmpCode.Value;
                ViewState["code"] = txtEmpCode.Value;
                ViewEmployee viewEmployee = new ViewEmployee();
                EmployeeManager manager = new EmployeeManager();
                viewEmployee = manager.SearchEmployeeByCode(code);
                lblEmpStatus.Text = viewEmployee.Employee.Status;
                lblempName.Text = viewEmployee.Employee.Name;
                lblDesignation.Text = viewEmployee.Designation.Title;
                txtPresentDesig.Value = viewEmployee.Designation.Title;
                lblProject.Text = viewEmployee.Project.ProjectName;
                lblJoiningDate.Text = viewEmployee.Employee.JoiningDate.ToShortDateString();
                lblBirth.Text = viewEmployee.Employee.DOB.ToShortDateString();
                empImage.ImageUrl = viewEmployee.Photo.FileName;

                //Employee Evaluation Info
                EvaluationManager evaluationManager = new EvaluationManager();
                SingleEmployeEvaluationReport report = new SingleEmployeEvaluationReport();
                report = evaluationManager.GetEmployeesEvaluation(code);
                lblEvaluatedBy.Text = report.Pm;
                lblEvaDate.Text = report.Date;
                lblSkill.Text = report.Skill;
                lblCommunication.Text = report.Communication;
                lblPunctuality.Text = report.Punctuality;
                lblComments.Text = '"' + report.Comments + '"';
                PopulatePromotionHistory();
                ShowPunishmentHistory();
                switch (report.Skill)
                {
                    case "Low": skillImage.Src = "~/images/LowPerformance.png"; break;
                    case "Avarage": skillImage.Src = "~/images/AverageProgress.png"; break;
                    case "High": skillImage.Src = "~/images/HighProgress.png"; break;
                    default: skillImage.Alt = "Not Updated Yet"; break;
                }
                switch (report.Punctuality)
                {
                    case "Low": puncImage.Src = "~/images/LowPerformance.png"; break;
                    case "Avarage": puncImage.Src = "~/images/AverageProgress.png"; break;
                    case "High": puncImage.Src = "~/images/HighProgress.png"; break;
                    default: puncImage.Alt = "Not Updated Yet"; break;
                }
                switch (report.Communication)
                {
                    case "Low": commImage.Src = "~/images/LowPerformance.png"; break;
                    case "Avarage": commImage.Src = "~/images/AverageProgress.png"; break;
                    case "High": commImage.Src = "~/images/HighProgress.png"; break;
                    default: commImage.Alt = "Not Updated Yet"; break;
                }


            }
            catch (Exception ex)
            {
                lblSearch.Text = "Employee not found by this code. Please try again";
                lblSearch.ForeColor = Color.Red;
                firstinfoTb.Visible = false;
                punishment.Visible = false;
                promotedTb.Visible = false;
                promotionHistoryTb.Visible = false;
                imageDiv.Visible = false;
            }

        }

        private void ShowPunishmentHistory()
        {
            Code code = new Code();
            code.CodeName = ViewState["code"].ToString();
            List<ViewAllPunisedmentEmployee> viewAllPunisedmentEmployees = new List<ViewAllPunisedmentEmployee>();
            PunishmentManager manager = new PunishmentManager();
            viewAllPunisedmentEmployees = manager.GetAllPunishedEmployee(code);
            if(viewAllPunisedmentEmployees.Count>0)
            {
                punishmentGdv.DataSource = viewAllPunisedmentEmployees;
                punishmentGdv.DataBind();
                punishmentGdv.Visible = true;
                lblPunished.Visible = false;
            }
            else
            {
                lblPunished.Visible = true;
                lblPunished.ForeColor = Color.OrangeRed;
                lblPunished.Text = "Employee not Punished ever";
                punishmentGdv.Visible = false;

            }
        }

        protected void SavePromotion(object o, EventArgs e)
        {
            try
            {
                Promotion promotion = new Promotion();
                PromotionManager manager = new PromotionManager();
                Code code = new Code();
                code.CodeName = ViewState["code"].ToString();
                Employee employee = new Employee();
                employee.EmpCode = code;
                promotion.Employee = employee;
                promotion.Date = txtDate.Value;
                promotion.PreDesignation = txtPresentDesig.Value;
                promotion.PresentDesig = selectDesig.Value;
                if (manager.SavePromotion(promotion))
                {
                    lblPromotion.ForeColor = Color.Green;
                    lblPromotion.Text = "Employee has been Successfully Promoted as " + selectDesig.Value;
                    PopulateEmployeeInfo();
                    PopulatePromotionHistory();
                }
            }
            catch (Exception ex)
            {
                lblPromotion.ForeColor = Color.Red;
                lblPromotion.Text = ex.Message;
            }
        }

        private void PopulatePromotionHistory()
        {
            PromotionManager manager = new PromotionManager();
            List<Promotion> promotions = new List<Promotion>();
            Code code = new Code();
            code.CodeName = ViewState["code"].ToString();
            promotions = manager.GetEmpPromotionHistory(code);
            promotionHistoryGdv.DataSource = promotions;
            promotionHistoryGdv.DataBind();

            if (promotions.Count > 0)
            {
                lblCountPromotion.ForeColor = Color.Green;
                lblCountPromotion.Text = "Employee has been Promoted " + promotions.Count+" times";
            }
            else
            {
                lblCountPromotion.ForeColor = Color.OrangeRed;
                lblCountPromotion.Text = "Employee not yet Promoted before !";
            }
        }
    }
}